﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using Invoicing.Model.Results;
using Invoicing.Model.Members;
using Invoicing.Logic.Members;
namespace Invoicing.Web.Management.Areas.Members.Controllers
{
    [RouteArea("Members")]
    [RoutePrefix("Customer")]
    [Authorize]
    public class CustomerApiController : ApiController
    {
        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<CustomerModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                CustomerQuery sea = new CustomerQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.Key = request.Value<string>("key");
                sea.GetCategories = request.Value<bool>("getCategory");
                sea.CType = request.Value<int>("cType");
                sea.Categories = request.Value<string>("categories");
                sea.Status = request.Value<int>("customerStatus");
                sea.GetBalance = request.Value<bool>("getBalance");
                sea.GetDealStatistic = request.Value<bool>("getDealStatistic");
                var result = CustomerLogic.GetCustomers(sea);
                return new JsonEntitySet<CustomerModel>(result);
            }
            catch (Utils.InvoicingException lde)
            {
                errinfo = lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<CustomerModel>(null, errinfo, false);
        }

        [Route("Save")]
        [HttpPost]
        public JsonEntity<CustomerModel> Save(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                CustomerModel model = new CustomerModel() { Categories=new CustomerCategoryCollection()};
                model.ID = request.Value<int>("customerID");
                model.Corporation = request.Value<string>("corporation");
                model.RealName = request.Value<string>("realname");
                model.Address = request.Value<string>("address");
                model.Mobile = request.Value<string>("mobile");
                model.Telephone = request.Value<string>("telephone");
                model.BankAccount = request.Value<string>("bankAccount");
                model.DeliverInfo = request.Value<string>("deliverInfo");
                model.CType = request.Value<int>("cType");
                var categories = request.Value<string>("categories");
                var catArray = categories.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (catArray.Length > 0)
                {
                    foreach (var c in catArray)
                    {
                        model.Categories.Add(new CustomerCategoryModel() { ID = int.Parse(c) });
                    }
                }
                model.Status = request.Value<int>("status");
                model.SortIndex = request.Value<int>("sortIndex");
                #endregion

                if (model.ID == 0)
                    model = CustomerLogic.CreateNew(model);
                else
                    CustomerLogic.Update(model);
                return new JsonEntity<CustomerModel>(model);
            }
            catch (Utils.InvoicingException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<CustomerModel>(null, errinfo, false);
        }

        [Route("Delete")]
        [HttpPost]
        public JsonResult<bool> Delete(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var id = request.Value<int>("customerID");
                #endregion
                CustomerLogic.Delete(id);
                return new JsonResult<bool>(true);
            }
            catch (Utils.InvoicingException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
